if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[MapChangedAnimalProtocolRows]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[MapChangedAnimalProtocolRows]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



/***************************************************************************
* Name:			MapChangedAnimalProtocolRows
* Purpose:		Sets a 'HasChanged' flag on the Staging table to determine if a field
*				on that agreement has changed since the last time the feed was run.
*
***************************************************************************/
CREATE PROCEDURE dbo.MapChangedAnimalProtocolRows
AS
BEGIN

	SET NOCOUNT ON

	BEGIN
		Update Stg_animalProtocol Set HasChanged = 1 WHERE
		IsCurrent = 1 AND
		ProtocolId is not null AND (
			(ISNULL(PIAdmPersonId, 0) != ISNULL(ExistingPIPersonId, 0)) OR
			--((ExistingProtocolTitle != sProtocolTitle) OR
			(ExistingProtocolShortTitle != sProtocolShortTitle) OR
			(ISNULL(ExistingReceivedDate, '') != ISNULL(dtReceived, '')) OR
			(ISNULL(OverallStatusId, '') != ISNULL(ExistingProtocolStatusTypeID	, '') ) OR
			(ISNULL(AdmSponsorId, 0) != ISNULL(ExistingAdmSponsorId, 0)))
	END
		
	SET NOCOUNT OFF

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO